package api;


import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ZKset {

    String ip = "azyjz.ltd:2181";
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper= new ZooKeeper(ip, 50000, new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("创建连接成功");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }

    @Test
    public void set1() throws  Exception{
        // 同步更新节点
        // arg1:节点的路径
        // arg2:节点修改的数据
        // arg3:版本号 -1代表版本号不作为修改条件
        Stat stat = zooKeeper.setData("/create/node1", "nnode1".getBytes(), 1);
        //节点版本号
        System.out.println(stat.getVersion());
        //节点创建时间
        System.out.println(stat.getCtime());

    }

    @Test
    public void set2() throws  Exception{
        // 异步步更新节点
        // arg1:节点的路径
        // arg2:节点修改的数据
        // arg3:版本号 -1代表版本号不作为修改条件
        // arg4:异步回调接口
        zooKeeper.setData("/create/node1", "nnode1".getBytes(), 2, new AsyncCallback.StatCallback() {
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                // 0 代表修改成功
                System.out.println(rc);
                // 修改节点的路径
                System.out.println(path);
                // 上线文的参数对象
                System.out.println(ctx);
                // 的属性信息
                System.out.println(stat.getVersion());
            }
        },"im context");


    }

    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }
}
